<template>
  <div class="billingDetails">
    <!-- 结算明细 -->
    <!-- 搜索部分 -->
    <div class="search">
      <el-select v-model="type" placeholder="请选择销售类型" size="mini">
        <el-option label="全部" value="-1"></el-option>
        <el-option label="新店购买" value="0"></el-option>
        <el-option label="店铺续费" value="1"></el-option>
        <el-option label="店铺升级" value="2"></el-option>
      </el-select>
      <div>
        <el-input
          size="mini"
          v-model="search"
          style="width: 270px"
          placeholder="请输入订单号/商户姓名/商铺id/商户账号"
          clearable
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-button
          size="mini"
          disable
          type="primary"
          @click="handleSearch"
          style="padding: 8px 20px;margin-left: 10px;border: none;color: #626366;background-color: #f1f1f1"
        >搜索</el-button>
      </div>
    </div>
    <!-- 订单统计 -->
    <Statistics :totalMoney="totalMoney" :orderLength="orderLength"></Statistics>
    <!-- 表格部分 -->
    <div class="tableData">
      <el-table style="width: 100%;margin-bottom: 40px" :data="tableData">
        <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" min-width="90" label="订单号" prop="sv_order_id"></el-table-column>
        <el-table-column align="center" min-width="120" label="店铺id">
          <template slot-scope="scope">
            <div class="contentToDraw">
              <span>{{ scope.row.user_id}}</span>
              <span v-if="scope.row.sv_salestype=='新'" class="dec">{{scope.row.sv_salestype}}</span>
              <span
                v-else-if="scope.row.sv_salestype=='续'"
                style="background: #ffa500"
                class="dec"
              >{{scope.row.sv_salestype}}</span>
              <span
                v-else="scope.row.sv_salestype=='升'"
                style="background: #67c23a"
                class="dec"
              >{{scope.row.sv_salestype}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          label="店铺名称"
          prop="sv_us_name"
        ></el-table-column>
        <el-table-column align="center" min-width="120" label="商户账号" prop="sv_ul_acountno"></el-table-column>
        <el-table-column
          align="center"
          label="商户姓名"
          :show-overflow-tooltip="true"
          prop="sv_ul_name"
        ></el-table-column>
        <el-table-column align="center" min-width="120" label="订单信息" prop="sv_order_content"></el-table-column>
        <el-table-column align="center" label="代币" prop="sv_receivable"></el-table-column>
        <el-table-column align="center" label="结算" prop="sv_payable"></el-table-column>
        <el-table-column align="center" label="代理级别" prop="sv_dl_name"></el-table-column>
        <el-table-column align="center" label="订单状态">
          <template slot-scope="scope">
            <span
              :style="scope.row.sv_orderstatu === '已完成' ? 'color: inherit' : 'color: #ff0000'"
            >{{scope.row.sv_orderstatu}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="付款时间"
          prop="sv_ordercreationdate"
          min-width="160"
          :formatter="dateFormat"
        ></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <!-- 判断是否超过十五天时限 -->
            <el-button
              :style="{display: (new Date(scope.row.sv_paymentdate).getTime() < new Date().getTime() - 86400000 * 15) ? 'none' : 'block'}"
              size="mini"
              type="primary"
              @click="handleRecycle(scope.row)"
            >撤销</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器部分 -->
    <Pagination
      :total="total"
      :pageIndex="pageIndex"
      :pageSize="pageSize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    ></Pagination>
  </div>
</template>

<script>
import Statistics from "@/components/Statistics";
import Pagination from "@/components/Pagination";
export default {
  data() {
    return {
      // 表单数据
      type: "",
      // 表格数据
      tableData: [],
      // 订单类型
      salestype: ["新", "续", "升"],
      // 订单状态
      orderstatu: ["已完成", "未完成", "退款"],
      search: "",
      // 总金额
      totalMoney: 0,
      // 分页器的数据
      total: 0, // 总条数
      pageIndex: 1, // 当前页
      pageSize: 10, // 每页数量
      // 每页订单数量
      orderLength: "",
      // 身份令牌
      myToken: ""
    };
  },
  components: {
    Pagination,
    Statistics
  },
  methods: {
    init() {
      // 获取令牌
      this.myToken = sessionStorage.getItem("myToken");
      // 初始化订单列表
      this.getOrderData();
    },
    // 获取订单数据
    getOrderData() {
      this.$api
        .getBillingDetailsList(
          this.pageIndex,
          this.pageSize,
          this.search.trim(),
          this.type,
          this.myToken
        )
        .then(res => {
          // 重置金额
          this.totalMoney = 0;
          if (res.succeed) {
            const { dataList, total } = res.values;
            if (dataList == null) {
              this.tableData = dataList;
              return;
            }
            this.orderLength = dataList.length;
            // 总数
            this.total = total;
            this.tableData = dataList.map(v => {
              if (v.sv_salestype == 0) {
                // 订单类型
                v.sv_salestype = this.salestype[0];
              } else if (v.sv_salestype == 1) {
                v.sv_salestype = this.salestype[1];
              } else {
                v.sv_salestype = this.salestype[2];
              }
              // 修改状态
              if (v.sv_orderstatu === 1) {
                v.sv_orderstatu = this.orderstatu[0];
              } else if (v.sv_orderstatu === 2) {
                v.sv_orderstatu = this.orderstatu[0];
              } else {
                v.sv_orderstatu = this.orderstatu[2];
              }
              this.totalMoney += v.sv_receivable;
              return {
                ...v,
                sv_order_id: v.sv_order_id.substring(v.sv_order_id.length - 8)
              };
            });
            this.totalMoney = +this.totalMoney.toFixed(2);
          }
        });
    },
    // 账号回收
    handleRecycle(val) {
      this.$api
        .backoutOrder(val.user_id, val.order_id, this.myToken)
        .then(res => {
          const { values, succeed } = res;
          if (succeed) {
            if (values) {
              this.$message({
                type: "success",
                message: values
              });
              this.getOrderData();
            } else {
              this.$message.error("操作失败");
            }
          } else {
            if (values) {
              this.$message.error(values);
            } else {
              this.$message.error("操作失败");
            }
          }
        });
    },
    // 分页器的方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getOrderData();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getOrderData();
    },
    //时间格式化
    dateFormat: function(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return this.$moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    // 搜索
    handleSearch() {
      this.pageIndex = 1;
      this.getOrderData();
    }
  },
  mounted() {
    // 初始化
    this.init();
  }
};
</script>

<style lang="less" scoped>
@deep: ~">>>";
.billingDetails {
  background-color: #fff;
  padding: 20px;
  .search {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
  }
  .tableData {
    .contentToDraw {
      .dec {
        background: #409eff;
        color: #fff;
        padding: 2px 8px;
      }
    }
  }
  // 更改表头颜色
  .tableData @{deep} .el-table th {
    background-color: #fafafa;
    color: #252525;
    font-weight: 100;
  }
}
</style>